import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../data/database/app_database.dart';
import '../../data/datasources/local/local_data_source.dart';
import '../../data/datasources/local/auth_local_data_source.dart';
import '../../data/datasources/remote/auth_api_service.dart';
import '../../data/datasources/remote/project_api_service.dart';
import '../../data/datasources/remote/note_api_service.dart'; // 添加笔记API服务
import '../../data/datasources/remote/todo_api_service.dart'; // 添加待办API服务
import '../../data/repositories/auth_repository_impl.dart';
import '../../data/repositories/project_repository_impl.dart';
import '../../data/repositories/note_repository.dart'; // 添加笔记仓储
import '../../data/repositories/todo_repository.dart'; // 添加待办仓储
import '../../domain/usecases/auth_usecases.dart';
import '../../domain/repositories/auth_repository.dart';
import '../../domain/repositories/project_repository.dart';
import '../../domain/repositories/todo_repository.dart';
import '../../domain/repositories/note_repository.dart';
import '../../core/network/dio_config.dart';
import '../../core/offline/offline_mode_manager.dart'; // 添加离线模式管理器
import '../../core/offline/offline_data_manager.dart'; // 添加离线数据管理器

// ============ 数据层依赖 ============

/// 数据库依赖
final databaseProvider = Provider<AppDatabase>((ref) {
  return AppDatabase();
});

/// 本地数据源依赖
final localDataSourceProvider = Provider<LocalDataSource>((ref) {
  final database = ref.read(databaseProvider);
  return LocalDataSource(database);
});

/// 认证本地数据源依赖
final authLocalDataSourceProvider = Provider<AuthLocalDataSource>((ref) {
  return AuthLocalDataSource();
});

/// Dio实例依赖
final dioProvider = Provider<Dio>((ref) {
  final authLocalDataSource = ref.read(authLocalDataSourceProvider);
  return DioConfig.createDio(() async {
    try {
      await authLocalDataSource.initialize();
      return await authLocalDataSource.getToken();
    } catch (e) {
      // 静默处理错误
      return null;
    }
  });
});

/// 认证API服务
final authApiServiceProvider = Provider<AuthApiService>((ref) {
  final dio = ref.read(dioProvider);
  return AuthApiService(dio);
});

/// 项目API服务
final projectApiServiceProvider = Provider<ProjectApiService>((ref) {
  final dio = ref.read(dioProvider);
  return ProjectApiService(dio);
});

/// 笔记API服务
final noteApiServiceProvider = Provider<NoteApiService>((ref) {
  final dio = ref.read(dioProvider);
  return NoteApiService(dio);
});

/// 待办API服务
final todoApiServiceProvider = Provider<TodoApiService>((ref) {
  final dio = ref.read(dioProvider);
  return TodoApiService(dio);
});

// ============ 离线功能依赖 ============

/// 离线模式管理器
final offlineModeManagerProvider = Provider<OfflineModeManager>((ref) {
  return OfflineModeManager();
});

/// 离线数据管理器
final offlineDataManagerProvider = Provider<OfflineDataManager>((ref) {
  return OfflineDataManager();
});

// ============ 仓储层依赖 ============

/// 认证仓储
final authRepositoryProvider = Provider<AuthRepository>((ref) {
  final apiService = ref.read(authApiServiceProvider);
  final localDataSource = ref.read(authLocalDataSourceProvider);
  return AuthRepositoryImpl(apiService, localDataSource);
});

/// 项目仓储
final projectRepositoryProvider = Provider<ProjectRepository>((ref) {
  final apiService = ref.read(projectApiServiceProvider);
  final localDataSource = ref.read(localDataSourceProvider);
  final offlineModeManager = ref.read(offlineModeManagerProvider);
  return ProjectRepositoryImpl(apiService, localDataSource, offlineModeManager);
});

/// 待办仓储
final todoRepositoryProvider = Provider<TodoRepository>((ref) {
  final apiService = ref.read(todoApiServiceProvider);
  final localDataSource = ref.read(localDataSourceProvider);
  final offlineModeManager = ref.read(offlineModeManagerProvider);
  final offlineDataManager = ref.read(offlineDataManagerProvider);
  return TodoRepositoryImpl(
    apiService: apiService,
    localDataSource: localDataSource,
    offlineModeManager: offlineModeManager,
    offlineDataManager: offlineDataManager,
  );
});

/// 笔记仓储
final noteRepositoryProvider = Provider<NoteRepository>((ref) {
  final apiService = ref.read(noteApiServiceProvider);
  final localDataSource = ref.read(localDataSourceProvider);
  final offlineModeManager = ref.read(offlineModeManagerProvider);
  final offlineDataManager = ref.read(offlineDataManagerProvider);
  return NoteRepositoryImpl(
    apiService,
    localDataSource,
    offlineModeManager,
    offlineDataManager,
  );
});

// ============ 业务逻辑层依赖 ============

/// 认证用例
final authUseCasesProvider = Provider<AuthUseCases>((ref) {
  final authRepository = ref.read(authRepositoryProvider);
  return AuthUseCases(authRepository);
});

// ============ 共享偏好设置 ============

/// 共享偏好设置
final sharedPreferencesProvider = Provider<SharedPreferences>((ref) {
  throw UnimplementedError('SharedPreferences should be overridden');
});